<?php
function mailtocommenter_options_page(){
	$options= get_option( 'mailtocommenter_options');
	$description = '';
	$mail_content = '';
	$url = get_option('siteurl').'/wp-admin/options-general.php?page=mailtocommenter.php';
	if (isset($_POST['update_setting'])) {
		$options['at_all'] = $_POST['at_all'];
		$options['at_all_permission'] = $_POST['at_all_permission'];
		$options['button_content'] =  $_POST['button_content'];
		$options['button_display'] = $_POST['button_display'];
		$options['button_customize_html'] = stripslashes($_POST['button_customize_html']);
		$options['button_title'] = $_POST['button_title'];
		$options['display_description'] = $_POST['display_description'];
		$options['description'] = stripslashes($_POST['description']);
		$options['notify_admin'] =  $_POST['notify_admin'];
		if(!$_POST['mail_notify']) $options['notify_admin']=false;
		$options['permission'] = $_POST['permission'];
		$options['mail_content'] = stripslashes($_POST['mail_content']);
		$options['mail_notify'] =$_POST['mail_notify'];
		$options['mail_subject'] = $_POST['mail_subject'];
		$options['save_options'] =  $_POST['save_options'];
		$options['type'] = $_POST['type'];
		$options['button_auto'] = $_POST['button_auto'];
		$options['button_commentthreading'] = $_POST['button_commentthreading'];
		$options['button_linkstyle'] = $_POST['button_linkstyle'];
		$options['mail_from'] = $_POST['mail_from'];
		update_option('mailtocommenter_options', $options);
		echo '<div id="message" class="updated fade"><p>';
		echo _e("Configuration updated.",'mailtocommenter');
		echo '</p></div>';
	}else if (isset($_POST['set_default'])) {
		$options = mailtocommenter_init_options();
		update_option('mailtocommenter_options', $options);
		echo '<div id="message" class="updated fade"><p>';
		echo _e("Default configuration loaded",'mailtocommenter');
		echo '</p></div>';		    
	}else if (isset($_POST['send_mail'])){
		$to = strtolower($_POST['to']);
		$subject = $_POST['subject'];
		$message =$_POST['message'];
		if(mailtocommenter_send_email($to,$subject,$message)){
			echo '<div id="message" class="updated fade"><p>';
			echo _e("Mail sent.",'mailtocommenter');
			echo '</p></div>';	
		}else{
			echo '<div id="message" class="updated fade"><p>';
			echo _e("Failed to send test mail.",'mailtocommenter');
			echo '</p></div>';
		}
	}else if (isset($_POST['preview'])){
		$options['mail_subject'] = $_POST['mail_subject'];
		$options['mail_content'] = stripslashes($_POST['mail_content']);
		update_option('mailtocommenter_options', $options);
		echo '<div id="message" class="updated fade">';
		echo '<b>'._e("Mail Subject:",'mailtocommenter').'</b><br/>';
		echo $options['mail_subject']."<hr/>";
		echo '<b>'._e("Mail Content:",'mailtocommenter').'</b><br/>';
		echo $options['mail_content'];
		echo '</div>';	
	}else if(isset($_POST['language'])) {
		$options['language'] = $_POST['language'];
		update_option('mailtocommenter_options', $options);
		echo '<div id="message" class="updated fade"><p>';
		echo _e("Language setting updated. Waiting for page to be reloaded...",'HotFriendstext');
		echo '</p></div>';
		echo '<script language="javascript">setTimeout("window.open(\''.$url.'\',\'_self\');",1000);</script>';
	}
?>
<div class="wrap">
	<h2>Mail to Commenter Options</h2>
	<p style="text-align:right"><a target="_blank" href="http://wordpress.org/extend/plugins/profile/redhorsecxl"><?php _e("Try other plugins developed by author in official Wordpress plugin repository!",'mailtocommenter');?></a></p>
	<form name="selectlanguage" method="post">
		<p><b><?php _e('Select Language: ','mailtocommenter');?></b>
		<input style="margin-left:10px" type="radio" name="language" value="0" <?php echo ($options['language'] == "0")?'checked="checked"':'';?> onchange="document.selectlanguage.submit();">English
		<input style="margin-left:10px" type="radio" name="language" value="1" <?php echo ($options['language'] == "1")?'checked="checked"':'';?> onchange="document.selectlanguage.submit();">简体中文
		<input style="margin-left:10px" type="radio" name="language" value="2" <?php echo ($options['language'] == "2")?'checked="checked"':'';?> onchange="document.selectlanguage.submit();">日本語
		<input style="margin-left:10px" type="radio" name="language" value="3" <?php echo ($options['language'] == "3")?'checked="checked"':'';?> onchange="document.selectlanguage.submit();">Belorussian
		</p>
	</form>
	<form method="post">
		<div class="submit"  style="text-align:right;margin-right:10%;border:none;padding-top:0px;padding-bottom:10px;border-bottom:1px solid #DADADA;">
			<input type="submit" name="set_default" value="<?php _e('Load default','mailtocommenter');?>" />
			<input type="submit" name="update_setting" value="<?php _e('Update setting','mailtocommenter');?>" />
		</div>
		<div style="border-bottom:1px solid #DADADA">
			<p><label><input name="mail_notify" type="checkbox" class="checkbox" <?php echo ($options['mail_notify'])?'checked="checked"':''; ?> /> <b><?php _e("Activate mail notification.",'mailtocommenter');?></label></b>
				<br/><span style="margin-left:1.5em"><small><?php _e("Checked will allow Wordpress to automatically send mail if comment content contains following specified code.",'mailtocommenter');?></small></span>
			</p>
			<p><b><?php _e("Code type:",'mailtocommenter');?></b>
				<label style="margin-left:10px"><input type="radio" <?php echo ($options['type'] == "@+user+blank")?'checked="checked"':''; ?> class="tog" value="@+user+blank" name="type"/> <?php _e('\'@+user+blank\'','mailtocommenter');?></label>
				<label style="margin-left:10px"><input type="radio" <?php echo ($options['type'] == "@+user+:")?'checked="checked"':''; ?> class="tog" value="@+user+:" name="type"/> <?php _e('\'@+user+:\'','mailtocommenter');?></label>
			</p>
			<p><b><?php _e("Permission to use mail notification:",'mailtocommenter');?></b>
				<label style="margin-left:10px"><input type="radio" <?php echo ($options['permission'] == "admin")?'checked="checked"':''; ?> class="tog" value="admin" name="permission"/> <?php _e('Admin','mailtocommenter');?></label>
				<label style="margin-left:10px"><input type="radio" <?php echo ($options['permission'] == "anyone")?'checked="checked"':''; ?> class="tog" value="anyone" name="permission"/> <?php _e('Anyone','mailtocommenter');?></label>
			</p>
			<p><label><input name="notify_admin" type="checkbox" class="checkbox" <?php echo ($options['notify_admin'])?'checked="checked"':''; ?> /><b><?php _e("Notify Admin.",'mailtocommenter');?></b></label>
			<br/><span style="margin-left:1.5em"><small><?php _e("Checked will copy send last mail content with the sending result indicated to admin. Be sure that activate mail notification option must be checked.",'mailtocommenter');?></small></span>
			</p>
			<p><label><input name="at_all" type="checkbox" class="checkbox" <?php echo ($options['at_all'])?'checked="checked"':''; ?> /><b><?php _e("Activate '@all ' or '@all:'.",'mailtocommenter');?></b></label>
			<br/><small><?php _e("Checked will allow user to use @all or @all: to send comment to all previous commenter. This option may make mail inundant to commenter.",'mailtocommenter');?></small>
			</p>
			<p style="margin-left:1.5em"><b><?php _e("Permission for using @all or @all:",'mailtocommenter');?></b>
				<label style="margin-left:10px"><input type="radio" <?php echo ($options['at_all_permission'] == "anyone")?'checked="checked"':''; ?> class="tog" value="anyone" name="at_all_permission"/> <?php _e('Anyone','mailtocommenter');?></label>
				<label style="margin-left:10px"><input type="radio" <?php echo ($options['at_all_permission'] == "admin")?'checked="checked"':''; ?> class="tog" value="admin" name="at_all_permission"/> <?php _e('Admin','mailtocommenter');?></label>
			</p>
			<p><label><input name="display_description" type="checkbox" class="checkbox" <?php echo ($options['display_description'])?'checked="checked"':''; ?> /><b><?php _e("Display following description under comment form. Html code supported.",'mailtocommenter');?></b></label>
				<ol style="margin-left:3em">
					<li><small><?php _e("Content in the form beneath is the output of function mailtocommenter_description(). Checked will diplay the following description under comment form.",'mailtocommenter');?></small></li>
					<li><small><?php _e("You also can specify the position of description. To do this, please keep option unchecked and place <span style=\"background-color:#EDEDFF\">&lt;?php if(function_exists('mailtocommenter_description')) mailtocommenter_description();?&gt;</span> into theme file.",'mailtocommenter');?></small></li>
				</ol>
				<textarea name="description" style="width: 800px; height: 150px;"><?php echo $options['description'];?></textarea>
			</p>
		</div>
		<div style="border-bottom:1px solid #DADADA">
			<p><span style="font-weight:bold;font-size:1.2em"><?php _e("Customize Button style:",'mailtocommenter');?></span><br/>
				<span style="margin-left:1.5em"><small><?php _e("This section configures the style of button generated by function mailtocommenter_button().",'mailtocommenter');?></small></span></p>
			<p><b><?php _e("Automatically insert reply button after comment:",'mailtocommenter');?></b>
				<label style="margin-left:10px"><input type="radio" <?php echo ($options['button_auto'] == 'yes')?'checked="checked"':''; ?> class="tog" value="yes" name="button_auto"/> <?php _e('Yes','mailtocommenter');?></label>
				<label style="margin-left:10px"><input type="radio" <?php echo ($options['button_auto'] == 'no')?'checked="checked"':''; ?> class="tog" value="no" name="button_auto"/> <?php _e('No','mailtocommenter');?></label>
			</p>
			<p style="margin-left:1.5em"><small><?php _e("Enable this option will automatically generate a reply button in the style specifed below after each comment, otherwise user needs to manually insert <span style=\"background-color:#EDEDFF\">&lt;?php if(function_exists('mailtocommenter_button')) mailtocommenter_button();?&gt;</span> into comment template file (normally comments.php) to display button properly.",'mailtocommenter')?></small>
			</p>
			<p><b><?php _e("Enable threaded comment",'mailtocommenter');?></b>
				<label style="margin-left:10px"><input type="radio" <?php echo ($options['button_commentthreading'] == 'yes')?'checked="checked"':''; ?> class="tog" value="yes" name="button_commentthreading"/> <?php _e('Yes','mailtocommenter');?></label>
				<label style="margin-left:10px"><input type="radio" <?php echo ($options['button_commentthreading'] == 'no')?'checked="checked"':''; ?> class="tog" value="no" name="button_commentthreading"/> <?php _e('No','mailtocommenter');?></label>
				<br/>
				<ol style="margin-left:3em">
					<li><small><?php _e("Enable this option will bond wordpress built-in comment threading function to reply button generated by mailtocommenter. It is only available after 2.8 versions.",'mailtocommenter');?>
					<li><small><?php _e("To use this option, make sure the threaded comment is enabled.",'mailtocommenter');?></small>
						<small><i><?php _e("Settings-->Discussion-->other comment settings--> Enable threaded (nested) comments.",'mailtocommenter');?></i></small>
					</li>
				</ol>
			</p>
			<p><b><?php _e("@user link format",'mailtocommenter');?></b>
				<label style="margin-left:10px"><input type="radio" <?php echo ($options['button_linkstyle'] == 'fulllink')?'checked="checked"':''; ?> class="tog" value="fulllink" name="button_linkstyle"/> <?php _e('Full link','mailtocommenter');?></label>
				<label style="margin-left:10px"><input type="radio" <?php echo ($options['button_linkstyle'] == 'simplifiedlink')?'checked="checked"':''; ?> class="tog" value="simplifiedlink" name="button_linkstyle"/> <?php _e('Simplified link','mailtocommenter');?></label>
				<ol style="margin-left:3em">
					<li><small><?php _e("Full link: post or page permalink+#comment-comment_ID, e.g., http://www.abc.com/post.html#comment-1. If comments are dispayed in pages, please use 'full link' to ensure links correctly direct to the replied comments.",'mailtocommenter');?>
						<b><i><?php _e("However, using 'full link' may cause the broken links if the structure of permalink is changed in future.",'mailtocommenter');?></i></b></small></li>
					<li><small><?php _e("Simplified link: #comment-comment_ID, e.g., #comment-1.",'mailtocommenter');?></small></li>
				</ol>
			</p>
			<p><span style="font-weight:bold"><?php _e("<b>Button Style</b>:",'mailtocommenter');?></span><br/>
			<small><?php _e("Specify the output of function mailtocommenter_button(). Html code supported. Default is to use notify.png image file in plugin folder, you can specify other image file or use plain text, like [reply]:",'mailtocommenter');?>
			</small>
			</p>
			
			<li style="margin-left:1.5em"><?php _e("Display Image:",'mailtocommenter');?>
				<p style="margin-left:1.5em">
					<?php $imgfile = get_option(siteurl).'/wp-content/plugins/mailtocommenter/notify.png';?>
					<input type="radio" <?php echo ($options['button_display'] == 1)?'checked="checked"':''; ?> class="tog" value="1" name="button_display"/>&nbsp;
					<?php echo "<a href=\"#\"><img src=\"$imgfile\" alt=\"Notify\"/></a>"?>&nbsp;&nbsp;<?php _e("Default display. @ image in plugin folder with link.",'mailtocommenter');?>
				</p>
				<p style="margin-left:1.5em">
					<?php $imgfile = get_option(siteurl).'/wp-content/plugins/mailtocommenter/reply.png';?>
					<input type="radio" <?php echo ($options['button_display'] == 2)?'checked="checked"':''; ?> class="tog" value="2" name="button_display"/>&nbsp;
					<?php echo "<a href=\"#\"><img src=\"$imgfile\" alt=\"Notify\"/></a>"?>&nbsp;&nbsp;<?php _e("Compatible to @reply. Reply image in plugin folder with link.",'mailtocommenter');?>
				</p>
			</li>
			<li style="margin-left:1.5em"><?php _e("Display Text:",'mailtocommenter');?>
				<p style="margin-left:1.5em">
					<input type="radio" <?php echo ($options['button_display'] == 3)?'checked="checked"':''; ?> class="tog" value="3" name="button_display"/>&nbsp;
					<a href="#"><?php _e('[Reply]','mailtocommenter');?></a>
				</p>
			</li>
			<li style="margin-left:1.5em"><?php _e("Customize Style:",'mailtocommenter');?>
				<p style="margin-left:1.5em">
					<input type="radio" <?php echo ($options['button_display'] == 0)?'checked="checked"':''; ?> class="tog" value="0" name="button_display"/>&nbsp;
					<?php _e("Specify the html code for button style. See the examples below.",'mailtocommenter');?>
				</p>
				<textarea style="margin-left:1.5em;width: 800px;height:30px" name="button_customize_html"><?php echo $options['button_customize_html'];?></textarea>
			</li>
			<p style="margin-left:1.5em"><?php _e("<b>Example</b>:",'mailtocommenter');?></p>
			<ol style="margin-left:4em">
				<li><a href="#"><img src="http://www.google.com/accounts/gmail20x20.gif" alt="Notify"/></a>,&nbsp;&nbsp;<?php _e("HTML code:",'mailtocommenter');?><span style="background-color:#EDEDFF">&lt;img src=&quot;http://www.google.com/accounts/gmail20x20.gif&quot; alt=&quot;Notify&quot;/&gt;</span>
				</li>
				<li><a href="#">!!<?php _e("Reply",'mailtocommenter');?>!!</a>,&nbsp;&nbsp;<?php _e("HTML code:",'mailtocommenter');?><span style="background-color:#EDEDFF">!!<?php _e("Reply",'mailtocommenter');?>!!</span></li>
			</ol>
			<p><?php _e("<b>Button title display</b>, content which displayed when user hovers mouse over button:",'mailtocommenter');?><br/>
				<input style="width: 800px;" name="button_title" type="textarea" value="<?php echo $options['button_title'];?>" />
			</p>
			<p><?php _e("<b>Content generated by click button</b>",'mailtocommenter');?>
				<ul>
					<li><label style="margin-left:10px"><input type="radio" <?php echo ($options['button_content'] == "Hyperlink")?'checked="checked"':''; ?> class="tog" value="Hyperlink" name="button_content"/> <?php _e('Hyperlink. Link targets to the comment you want to reply. e.g, <a href="#">@User </a>.','mailtocommenter');?></label></li>
					<li><label style="margin-left:10px"><input type="radio" <?php echo ($options['button_content'] == "atreply")?'checked="checked"':''; ?> class="tog" value="atreply" name="button_content"/> <?php _e('@Reply style. i.e, @<a href="#">User </a>.','mailtocommenter');?></label></li>
					<li><label style="margin-left:10px"><input type="radio" <?php echo ($options['button_content'] == "plain text")?'checked="checked"':''; ?> class="tog" value="plain text" name="button_content"/> <?php _e('Plain text. e.g, @user .','mailtocommenter');?></label></li>
					<li><label style="margin-left:10px"><input type="radio" <?php echo ($options['button_content'] == "display nothing")?'checked="checked"':''; ?> class="tog" value="display nothing" name="button_content"/> <?php _e('Display nothing. i.e, @user will be encapsulated by html comment code &lt;!-- and --&gt;.','mailtocommenter');?></label></li>
				</ul>
			</p>
		</div>
		<div>
			<div style="float:left; width:610px">
				<p><span style="font-weight:bold;font-size:1.2em"><?php _e("Mail Template",'mailtocommenter');?></span><br/>
					<small><?php _e("User is encouraged to adpot predefined variable to customize owner mail template. See right side for explanation of predined variables. Html code supported.",'mailtocommenter');?></small>
				</p>
				<p><?php _e("Mail From:",'mailtocommenter');?><br/>
					<textarea style="width: 600px;height:30px" name="mail_from"><?php echo $options['mail_from'];?></textarea>
				</p>
				<p><?php _e("Mail Subject:",'mailtocommenter');?><br/>
					<textarea style="width: 600px;height:30px" name="mail_subject"><?php echo $options['mail_subject'];?></textarea>
				</p>
				<p><?php _e("Mail Content:",'mailtocommenter');?><br/>
					<textarea style="width: 600px;height:300px;" name="mail_content" type="textarea"><?php echo $options['mail_content'];?></textarea>
					<br/><input type="submit" name="preview" value="<?php echo _e('Preview','mailtocommenter');?>"/><span style="margin-left:20px"><?php _e("<b>Advice:</b> Simply click Preview button to see the mail content style while editing.",'mailtocommenter');?></span>
				</p>
			</div>
			<div style="float:left;width:300px;margin-left:20px">
				<p><span style="font-weight:bold;font-size:1.2em"><?php _e("Meaning of predefined variables",'mailtocommenter');?></span><br/><br/>
					<b>%admin_email%:</b>&nbsp;&nbsp;<?php _e("Admin email",'mailtocommenter');?><br/>
					<b>%blog_name%:</b>&nbsp;&nbsp;<?php _e("Blog name",'mailtocommenter');?><br/>
					<b>%blog_link%:</b>&nbsp;&nbsp;<?php _e("Blog link",'mailtocommenter');?><br/>
					<b>%blog_host%:</b>&nbsp;&nbsp;<?php _e("Blog host",'mailtocommenter');?><br/>
					<b>%comment_author%:</b>&nbsp;&nbsp;<?php _e("Comment author, the one insert @user ",'mailtocommenter');?><br/>
					<b>%comment_link%:</b>&nbsp;&nbsp;<?php _e("Comment link",'mailtocommenter');?><br/>
					<b>%comment_time%:</b>&nbsp;&nbsp;<?php _e("Comment time",'mailtocommenter');?><br/>
					<b>%your_comment%:</b>&nbsp;&nbsp;<?php _e("Your comment, last comment of @user in post",'mailtocommenter');?><br/>
					<b>%post_link%:</b>&nbsp;&nbsp;<?php _e("Post link",'mailtocommenter');?><br/>
					<b>%post_title%:</b>&nbsp;&nbsp;<?php _e("Post title",'mailtocommenter');?><br/>
					<b>%reply_comment%:</b>&nbsp;&nbsp;<?php _e("Reply comment, comment of the one who insert @user",'mailtocommenter');?><br/>
					<b>%rss_name%:</b>&nbsp;&nbsp;<?php _e("RSS link display, default is RSS",'mailtocommenter');?><br/>
					<b>%rss_link%:</b>&nbsp;&nbsp;<span style="text-decoration:blink"><?php _e("RSS link, please specify your rss link since most people use third party (feedburner/feedsky) to manage rss.",'mailtocommenter');?></span><br/>
					<b>%user%:</b>&nbsp;&nbsp;<?php _e("User name displayed after @",'mailtocommenter');?><br/>
				</p>
				<div style="padding:10px 5px 10px 5px;border:1px solid #999;">
					<?php _e("<b>Note:</b><br/>1. Again, be sure the value of %rss_link% is correctly placed.",'mailtocommenter');?>
					<?php _e("<br/>2. If your <b>mail service doesn't support html format message</b>, please remove all html tag in template.",'mailtocommenter');?>
				</div>
			</div>
		</div>
		<div style="clear:both"></div>
		<div>
			<p><label><input name="save_options" type="checkbox" class="checkbox" <?php echo ($options['save_options'])?'checked="checked"':''; ?> /> <?php _e("Keep configurations of Mail To Commenter while plugin deactivation.",'mailtocommenter');?></label>
				<br/><small><?php _e("Unchecked will automatically remove all associated settings from database.",'mailtocommenter');?></small>
			</p>
		</div>
		<div class="submit"  style="text-align:right;margin-right:10%;border:none;padding-top:0px;padding-bottom:10px;border-bottom:1px solid #DADADA;">
			<input type="submit" name="set_default" value="<?php _e('Load default','mailtocommenter');?>" />
			<input type="submit" name="update_setting" value="<?php _e('Update setting','mailtocommenter');?>" />
		</div>
		<div>
			<p><?php _e("<b>Note</b>:") ?></p>
			<ol>
				<li><?php _e("User can use this section to test whether your wordpress works well with mail sending or not. If the receipter does not receive mail, your wordpress still may not send mail correctly even it says 'mail sent' ",'mailtocommenter');?></li>
				<li><?php _e("For the hosts which <b>do not support</b> php mail() function, it is recommended to install <a target='_blank' href='http://wordpress.org/extend/plugins/wp-mail-smtp/'>wp mail smtp</a> plugin to use smtp to send mail instead of mail().",'mailtocommenter');?></li>
			</ol>
			<p style="font-weight:bold;font-size:1.5em">
				<?php _e("Send Mail",'mailtocommenter');?><input style="margin-left:20px" type="submit" name="send_mail" value="<?php echo _e('Send','mailtocommenter');?>"/>
			</p>
			<p><?php _e("Address:",'mailtocommenter');?><br/><input style="width: 600px;" name="to" type="textarea" value="" /></p>
			<p><?php _e("Subject: ",'mailtocommenter');?><br/><input style="width: 600px;" name="subject" type="textarea" value="" /></p>
			<p><?php _e("Content:",'mailtocommenter');?><br/><textarea style="width: 600px;height:200px" name="message"></textarea></p>
		</div>
	</form>
	<p style="text-align:right"><a target="_blank" href="http://wordpress.org/extend/plugins/profile/redhorsecxl"><?php _e("Try other plugins developed by author in official Wordpress plugin repository!",'mailtocommenter');?></a></p>
</div>
<?php
}
function mailtocommenter_send_email($to,$subject,$message){
	$options= get_option( 'mailtocommenter_options');
	$from = $options['mail_from'];
	$charset = get_option('blog_charset');
	$headers  = 'From: '.$from."\r\n";
	if($options['notify_admin']){
		$admin_email = get_option('admin_email');
		$headers.= 'Bcc:'.$admin_email."\r\n" ;
	}
	$headers .= 'MIME-Version: 1.0'."\r\n";
	$headers .= 'Content-Type: text/html;charset="'.$charset.'"'."\r\n";
	$to = strtolower($to);
	return wp_mail($to, $subject, $message, $headers);
}
function mailtocommenter_get_hostname(){
	$blog_name = get_option('blogname');
	$blogurl = get_option('home'); 
	preg_match("/^(http:\/\/)?([^\/]+)/i",$blogurl, $matches);
	$blog_host = $matches[2];
	preg_match("/[^\.\/]+\.[^\.\/]+$/", $blog_host, $matches);
	return $matches[0];
}
function mailtocommenter_description(){
	$options= get_option( 'mailtocommenter_options');
	if($options['display_description']){
		print ($options['description']);
	}
}
function mailtocommenter_filter($content,$comment,$post,$commenter){
	$admin_email = get_option('admin_email');
	$blog_name = get_option('blogname');
	$blog_link = get_option('home');
	$comment_author = $comment['comment_author'];
	$post_link =  get_permalink($comment['comment_post_ID']);
	$comment_ID = $comment['comment_ID'];
	$comment_link = $post_link."#comment-$comment_ID";
	$comment_time = $comment['comment_date'];
	$your_comment = $commenter['comment_content'];
	$post_title =  $post->post_title;
	$reply_comment = $comment['comment_content'];
	$username = $commenter['comment_author'];
	
	$content= str_replace("%admin_email%",$admin_email,$content);
	$content= str_replace("%blog_name%",$blog_name,$content);
	$content= str_replace("%blog_link%",$blog_link,$content);
	$content= str_replace("%comment_author%",$comment_author,$content);
	$content= str_replace("%comment_link%",$comment_link,$content);
	$content= str_replace("%comment_time%",$comment_time,$content);
	$content= str_replace("%your_comment%",$your_comment,$content);
	$content= str_replace("%post_link%",$post_link,$content);
	$content= str_replace("%post_title%",$post_title,$content);
	$content= str_replace("%reply_comment%",$reply_comment,$content);
	$content= str_replace("%rss_name%","RSS",$content);
	$content= str_replace("%rss_link%",get_bloginfo_rss('rss2_url'),$content);
	$content= str_replace("%user%",$username,$content);
	return $content;
}

function mailtocommenter_generate_name($name){
	$options= get_option( 'mailtocommenter_options');
	if($options['type'] == '@+user+blank'){
			$to = "@$name ";
	}else{
			$to = "@$name:";
	}
	return $to;
}
function mailtocommenter_init_options(){
	$locale = get_locale();
	if($locale == "zh_CN"){
		$language = 1;
		load_textdomain('mailtocommenter', dirname(__FILE__) . "/mailtocommenter-zh_CN.mo");
	}elseif ($locale == "ja"){
		$language = 2;
		load_textdomain('mailtocommenter', dirname(__FILE__) . "/mailtocommenter-ja.mo");
	}elseif($locale == "by_BY"){
		load_textdomain('mailtocommenter', dirname(__FILE__) . "/mailtocommenter-by_BY.mo");
	}else{
		$language = 0;
		load_textdomain('mailtocommenter', dirname(__FILE__) . "/mailtocommenter-en_US.mo");
	}
	$blog_name = get_option('blogname');
	$admin_email = get_option('admin_email');
	$blog_host = mailtocommenter_get_hostname();
	$options = array(	
		'at_all' => false,
		'at_all_permission' => "admin",
		'button_auto' => "no",
		'button_commentthreading' => "no",
		'button_content' => "Hyperlink",
		'button_customize_html' => '',
		'button_display' => 1,
		'button_linkstyle' => "simplifiedlink",
		'button_title' => __("Notify this pumpkin.",'mailtocommenter'),
		'description' => __('<b>Note:</b> Commenter is allowed to use <b>\'@User+blank\'</b> to automatically notify your reply to other commenter. e.g, if ABC is one of commenter of this post, then write \'@ABC \'(exclude \') will automatically send your comment to ABC. Using \'@all \' to notify all previous commenters. Be sure that the value of User should exactly match with commenter\'s name (case sensitive).','mailtocommenter'),
		'display_description' => true,
		'language' => $language,
		'mail_content' => __("Mail content in different languages",'mailtocommenter'),
		'mail_from' => $blog_name." <no-reply@".$blog_host.">",
		'mail_notify' =>true,
		'mail_subject' => __('Your comment on [%blog_name%] just been replied by %comment_author%','mailtocommenter'),
		'notify_admin' => false,
		'permission' => 'admin',
		'save_options' => false,
		'type' => '@+user+blank',
	);
	return $options;		
}
function mailtocommenter_button(){
	echo mailtocommenter_generate_button();
}

function mailtocommenter_display_button($content){
	$options= get_option( 'mailtocommenter_options');
	global $comment;
	$reply = comment_reply_link($comment->comment_ID );
	if($options['button_auto'] == "yes"){	
		echo $content.'<br/>'.mailtocommenter_generate_button();
		echo '<br/>'.$reply;
	}else{
		echo $content;
	}
}
function mailtocommenter_generate_button(){
	global $comment;
	global $post;
	$options= get_option( 'mailtocommenter_options');
	$name = $comment->comment_author;
	$comment_ID = $comment->comment_ID;
	$value = mailtocommenter_generate_name($name);
	if($options['button_linkstyle'] == "fulllink"){
		$link = get_permalink( $comment->comment_post_ID );
		$link .= "#comment-$comment_ID";
	}else{
		$link ="#comment-$comment_ID";
	}
	if($options['button_content'] == "Hyperlink"){
		$value = "<a href=\"$link\">$value</a>";
	}elseif($options['button_content'] == "atreply"){
		$value = substr($value,1);
		$value = "@<a href=\"$link\">$value</a>";
	}elseif($options['button_content'] == "plain text"){
		$value = "$value";
	}else{
		$value = "<!--$value-->";
	}
	$value = htmlspecialchars($value);
	$title = $options['button_title'];
	$display = mailtocommenter_button_html();
	if($options['button_commentthreading'] == "yes") {
		if ( empty($post) ){
			$post = $comment->comment_post_ID;
			$post = get_post($post);
		}
		/*
		$onclick = "onclick = \"";
		$onclick .= "document.getElementById('comment').focus();document.getElementById('comment').value += '{$value}';";
		$onclick .= "return addComment.moveForm('comment-$comment_ID', '$comment_ID', 'respond', '$post->ID')\"";
		*/
		$onclick = "onclick = \"";
		$onclick .= "document.getElementById('comment').focus();document.getElementById('comment').value += '{$value}';";
		$onclick .= "return addComment.moveForm('comment-$comment_ID', '$comment_ID', 'respond', '$post->ID')\"";
		$button = "<a title='{$title}' class='comment-reply-link' href='" . esc_url( add_query_arg( 'replytocom', $comment_ID ) ) . "#respond'" .$onclick.">".$display."</a>";
	}else{
		$button =  "<a href=\"#commentform\" title=\"{$title}\" onclick=\"document.getElementById('comment').focus();document.getElementById('comment').value += '{$value}'\">{$display}</a>";
	}
	return $button;	
}
function mailtocommenter_button_html(){
	$options= get_option( 'mailtocommenter_options');
	$id = $options['button_display'];
	$imgfile = get_option(siteurl).'/wp-content/plugins/mailtocommenter';
	switch($id){
		case 0:
			$html = $options['button_customize_html'];
			break;
		case 1:
			$html = "<img src=\"$imgfile/notify.png\" alt=\"Notify\"/>";
			break;
		case 2:
			$html = "<img src=\"$imgfile/reply.png\" alt=\"Notify\"/>";
			break;
		case 3:
			$html = _e("[Reply]",'mailtocommenter');;
			break;
		default:
			$html = "<img src=\"$imgfile/notify.png\" alt=\"Notify\"/>";
	}
	if ((preg_match('/(src)/', $html)) and (preg_match('/(img)/', $html))) {
		return $html;
	} else {
		return htmlspecialchars($html);
	}
}
function mailtocommenter_getcomments($post_id){
	$comments = get_approved_comments($post_id);
	foreach($comments as $comment){
		$commenter = array();
		$id = (int)$comment->comment_ID;
		$commenter["comment_ID"] = $id;
		$commenter["comment_author"] = $comment->comment_author;
		$commenter["comment_content"] = $comment->comment_content;
		$commenter["comment_author_email"] = $comment->comment_author_email;
		$commenters["$id"] = $commenter; 
	}
	return ($commenters);
}
function mailtocommenter_getcommenters_old($options,$commentdata){
	global $wpdb;
	$commentcontent = $commentdata['comment_content'];
	$cid = (int)$commentdata['comment_ID'];
	$post_id = (int)$commentdata['comment_post_ID'];
	$comments = get_approved_comments($post_id);
	if($options['button_content'] == "plain text"){
		if( ($options['type'] == "@+user+blank") or ($options['button_content'] == "display nothing")){
			preg_match_all('/@([^\s]*)\s/', $commentcontent, $matches, PREG_PATTERN_ORDER);
		}else{
			preg_match_all('/@([^:]*):/', $commentcontent, $matches, PREG_PATTERN_ORDER);
		}
		$matches = $matches[1];
		$i = 0;
		foreach($matches as $name){
			$username = $matches[$i];
			$commenter = $wpdb->get_results("SELECT $wpdb->comments.comment_ID,$wpdb->comments.comment_author, $wpdb->comments.comment_author_email, $wpdb->comments.comment_content FROM $wpdb->comments WHERE $wpdb->comments.comment_post_ID='$post_id' AND $wpdb->comments.comment_author='$username' ORDER BY $wpdb->comments.comment_date DESC",ARRAY_A);
			$commenter = $commenter[0];
			if($commenter){
				$comment_ID = $commenter['comment_ID'];
				$commenters["$comment_ID"] = $commenter;	
			}
			$i++;
		}
	}else{
		if($options['button_content'] == "Hyperlink"){
			preg_match_all('/#comment-(\d+)[^^]*?@/', $commentcontent, $matches, PREG_PATTERN_ORDER);
		}elseif($options['button_content'] == "atreply"){
			preg_match_all('/@[^^]*?#comment-(\d+)"/', $commentcontent, $matches, PREG_PATTERN_ORDER);

		}
		$comment_IDs = $matches[1];
		$i = 0;
		foreach($comment_IDs as $comment_ID){
			$id = $comment_IDs[$i];
			$i++;
			$commenters["$id"] = $id;
		}
		foreach($comments as $comment){
			$id = (int)$comment->comment_ID;
			if(array_key_exists($id,$commenters)){
				$commenter = array();
				$commenter["comment_ID"] = $id;
				$commenter["comment_author"] = $comment->comment_author;
				$commenter["comment_content"] = $comment->comment_content;
				$commenter["comment_author_email"] = $comment->comment_author_email;
				$commenters["$id"] = $commenter;
			}
		}
	}
	return $commenters;
}
?>